package ce.com.cenewbluesdk.proxy;


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Log;

import ce.com.cenewbluesdk.CEBC;
import ce.com.cenewbluesdk.bluetooth.CEBlueToothBase;
import ce.com.cenewbluesdk.bluetooth.CEBlueToothBase.blueToothConnectStateChangeListen;
import ce.com.cenewbluesdk.connect.ReconnectMode;
import ce.com.cenewbluesdk.proxy.CEBluetoothProxyBase;
import ce.com.cenewbluesdk.uitl.CEBlueSharedPreference;

/**
 * Created by Administrator on 2016/11/7 0007.
 */
public class CEConnectUtil extends BroadcastReceiver implements blueToothConnectStateChangeListen {

    public static String Action_Reconnect = "reconnect_dev_action";
    /**
     * 判断是否注册的重连的广播
     * 如果注册的测不需要在注册
     */
    private boolean broadHaveregister = false;


    private int currConnectState;

    private Context context;

    /**
     * 蓝牙类型
     */
    private CEBlueToothBase blueTooth;
    /**
     * 设备代理
     */
    private CEBluetoothProxyBase proxyBase;
    /**
     * 蓝牙地址
     */
    private String blueAddress;


    /**
     * 回连机制
     */
    private ReconnectMode reconnectMode;

    @Override
    public void blueToothConnectStateChange(int state) {
        currConnectState = state;
        proxyBase.blueToothConnectStateChange(state);
        if (currConnectState == CEBC.BLUE_DISCONNECT) {
            if (!TextUtils.isEmpty(proxyBase.getBlueAddress())) {
                reconnectDev();
            } else if (reconnectMode.needTempConnect()) {
                reconnectDev();
            }

        } else if (currConnectState == CEBC.BLUE_CONNECTED) {
            reconnectMode.clearConnectTimes();
            reconnectMode.clearTempConnect();
        }
    }

    public CEConnectUtil(Context context, ReconnectMode reconnectMode, CEBluetoothProxyBase proxyBase, CEBlueToothBase blueTooth) {
        this.context = context;
        this.reconnectMode = reconnectMode;
        this.proxyBase = proxyBase;
        this.blueTooth = blueTooth;
        blueTooth.setBlueStateChangeListen(this);
        Action_Reconnect = Action_Reconnect + proxyBase.getClassName();


    }

    public void connect(String mac) {
        reconnectMode.resetTempConnect();
        reconnectMode.clearConnectTimes();
        blueAddress = mac;
        blueTooth.connect(mac);
        registerRece();
    }

    private void registerRece(){
        try {
            if (!broadHaveregister) {
                broadHaveregister = true;
                IntentFilter intentFilter = new IntentFilter();
                intentFilter.addAction(Action_Reconnect);
                context.registerReceiver(this, intentFilter);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void reConnect(String mac) {
        blueAddress = mac;
        blueTooth.connect(mac);
        registerRece();
    }

    public void disConnect() {
        try {
            if (broadHaveregister) {
                broadHaveregister = false;
                context.unregisterReceiver(this);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        blueTooth.disConnect();

    }

    public int getConnectTimes() {
        return reconnectMode.getConnectTimes();
    }

    public void reconnectDev() {
        Intent intent = new Intent(Action_Reconnect);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        PendingIntent mmCurrentAlarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        if (Build.VERSION.SDK_INT < 19) {
            am.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + reconnectMode.getConnectIntervalTime(), mmCurrentAlarmIntent);
        } else {
            am.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + reconnectMode.getConnectIntervalTime(), mmCurrentAlarmIntent);
        }

    }

    @Override
    public void onReceive(Context context, Intent intent) {

        reConnect(blueAddress);
        reconnectMode.addConnectTimes();

    }

    public int getCurrConnectState() {
        return currConnectState;
    }
}
